package cc.rengu.igas.cops.common.dao.impl;

import cc.rengu.igas.cops.common.dao.CopsOpmsUserInfoMapper;
import cc.rengu.igas.cops.common.entity.CopsOpmsUserInfo;
import cc.rengu.igas.cops.common.entity.CopsOpmsUserInfoAndSignAcctInfo;
import cc.rengu.igas.cops.common.enums.UserStatusEnum;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

public class CopsOpmsUserInfoMapperImpl implements CopsOpmsUserInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.OLTP_POOL_NAME);

    @Override
    public int insertOpmUserInfo(CopsOpmsUserInfo opmsUserInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CopsOpmsInsertUserInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, opmsUserInfo, opmsUserInfo);
        if (0 != iReturnCode) {
            rglog.error("用户注册登记用户表信息,iReturnCode:<{}>,userId:<{}>,nickname:<{}>",
                    iReturnCode, opmsUserInfo.getUserId(), opmsUserInfo.getUserName());
        }

        return iReturnCode;
    }


    @Override
    public int insertOpmUserInfoWithTrancation(CopsOpmsUserInfo opmsUserInfo, DbsUtil dbsUtil) throws Exception {
        /*XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);*/
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CopsOpmsInsertUserInfo");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, opmsUserInfo, opmsUserInfo);
        if (0 != iReturnCode) {
            rglog.error("用户注册登记用户表信息,iReturnCode:<{}>,userId:<{}>,nickname:<{}>",
                    iReturnCode, opmsUserInfo.getUserId(), opmsUserInfo.getUserName());
        }

        return iReturnCode;
    }


    @Override
    public CopsOpmsUserInfo selectOpmsSysUserInfoByInstIdUserInstUserId(String instId, String userInst, String userId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CopsOpmsGetUserInfoByInstIdUserInstUserId");

        CopsOpmsUserInfo info = new CopsOpmsUserInfo();
        info.setInstId(instId);
        info.setUserInst(userInst);
        info.setUserId(userId);

        int recode = dbsUtil.dbsExecuteSql(sqlStr, info, info);
        if (cc.rengu.utility.dbs.Database.DBS_NOT_FOUND == recode) {
            rglog.info("查询结果为空，instId:<{}>,userId:<{}>,userInst:<{}>,recode:<{}>", instId, userId, userInst, recode);
            return null;
        } else if (cc.rengu.utility.dbs.Database.DBS_SUCCESS != recode) {
            rglog.info("查询失败，instId:<{}>,userId:<{}>,userInst:<{}>,recode:<{}>", instId, userId, userInst, recode);
            return null;
        }
        return info;
    }


    @Override
    public CopsOpmsUserInfo selectOpmsSysUserInfoByInstIdUserInstHostNo(String instId, String userInst, String hostCustNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CopsOpmsSysUserInfoByInstIdUserInstHostNo");

        CopsOpmsUserInfo info = new CopsOpmsUserInfo();
        info.setInstId(instId);
        info.setUserInst(userInst);
        info.setCoreCustomerId(hostCustNo);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, info, CopsOpmsUserInfo.class);
        if (objectList.isEmpty()) {
            rglog.info("查询结果为空，instId:<{}>,hostCustNo:<{}>,userInst:<{}>", instId, hostCustNo, userInst);
            return null;
        }
        List<CopsOpmsUserInfo> userList = objectList.stream().map(item -> (CopsOpmsUserInfo) (item)).collect(Collectors.toList());
        //只去有效状态的用户信息
        userList = userList.stream().filter(item -> item.getUserStatus().equals(UserStatusEnum.NOMAL.getStatus())).collect(Collectors.toList());
        return userList.get(0);
    }


    @Override
    public CopsOpmsUserInfo selectOpmsSysUserInfoByUserAccountInfo(String instId, String userInst, String userName, String certType, String certId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CopsOpmsGetUserInfoByUserAccuntInfo");

        CopsOpmsUserInfo info = new CopsOpmsUserInfo();
        info.setInstId(instId);
        info.setUserInst(userInst);
        info.setUserName(userName);
        info.setCertType(certType);
        info.setCertNo(certId);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, info, CopsOpmsUserInfo.class);
        if (objectList.isEmpty()) {
            rglog.info("查询结果为空，instId:<{}>,userInst:<{}>,userName:<{}>,certType:<{}>,certId:<{}>", instId, userName, certType, certId);
            return null;
        }
        List<CopsOpmsUserInfo> userList = objectList.stream().map(item -> (CopsOpmsUserInfo) (item)).collect(Collectors.toList());
        //只去有效状态的用户信息
        userList = userList.stream().filter(item -> item.getUserStatus().equals(UserStatusEnum.NOMAL.getStatus())).collect(Collectors.toList());
        return userList.get(0);
    }

    @Override
    public int updateOpmsSysUserInfo(CopsOpmsUserInfo userInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsUpdateOpmsUserInfo");

        return dbsUtil.dbsExecuteSql(sqlStr, userInfo, userInfo);
    }

    @Override
    public PageResult selectCustomSignInfoByNotNullParams(String instId, String certType, String certNo, String mobile, String status, String startDate, String endDate, String signType, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsSelectCustomSignInfoByNotNullParams");
        CopsOpmsUserInfoAndSignAcctInfo copsOpmsUserInfoAndSignAcctInfo = new CopsOpmsUserInfoAndSignAcctInfo();
        copsOpmsUserInfoAndSignAcctInfo.setInstId(instId);
        copsOpmsUserInfoAndSignAcctInfo.setCertNo(certNo);
        copsOpmsUserInfoAndSignAcctInfo.setCertType(certType);
        copsOpmsUserInfoAndSignAcctInfo.setMobileNo(mobile);
        copsOpmsUserInfoAndSignAcctInfo.setUserStatus(status);
        copsOpmsUserInfoAndSignAcctInfo.setCreateTime(startDate);
        copsOpmsUserInfoAndSignAcctInfo.setLastUpdateTime(endDate);
        copsOpmsUserInfoAndSignAcctInfo.setSignType(signType);


        PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, copsOpmsUserInfoAndSignAcctInfo, CopsOpmsUserInfoAndSignAcctInfo.class, pageInfo);
        if (null == pageResult) {
            rglog.error("根据证件信息查询收银台用户信息失败,instId:<{}>,certNo:<{}>，certType=<{}>,mobileNo = <{}>,userStatus = <{}>,startDate = <{}>,endDate =<{}>", instId, certNo, certType, mobile, status, startDate, endDate);
            return null;
        }
        return pageResult;
    }

    @Override
    public CopsOpmsUserInfo selectOpmsSysUserInfoByCertType(String instId, String userInst, String certType, String certNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CopsOpmsGetUserInfoByCertType");

        CopsOpmsUserInfo info = new CopsOpmsUserInfo();
        info.setInstId(instId);
        info.setUserInst(userInst);
        info.setCertType(certType);
        info.setCertNo(certNo);

        List<Object> userList = dbsUtil.dbsFetchLimitListData(sqlStr, info, CopsOpmsUserInfo.class);
        if (null == userList || userList.isEmpty()) {
            rglog.info("无此用户信息签约主体<{}>,证件类型<{}>,证件号码", userInst, certType, certNo);
            return null;
        }
        return userList.stream().map(item -> (CopsOpmsUserInfo) item).collect(Collectors.toList()).get(0);
    }


}
